package com.fxcalibre.fxml.control;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;

import com.fxcalibre.fxml.control.listCells.TitleImdbCell;
import com.fxcalibre.fxml.view.FXMLViewLoader;
import com.fxcalibre.syncservices.imdb.ImdbTitleSearch;
import com.fxcalibre.syncservices.imdb.parser.TitleResult;

public class ImdbSearchTitleResult extends VBox
{
	Stage dialogStage;
	@FXML
	ListView<TitleResult> listResults;

	String title;
	TitleResult selectedTitleResult;

	ObservableList<TitleResult> data = FXCollections.observableArrayList();

	public ImdbSearchTitleResult()
	{
		System.out.println("ImdbSearchTitleResult-->");
		FXMLViewLoader.load(this, "ImdbSearchResults.fxml");
		listResults.setItems(data);
		listResults.setCellFactory(new Callback<ListView<TitleResult>, ListCell<TitleResult>>()
		{
			@Override
			public ListCell<TitleResult> call(ListView<TitleResult> list)
			{
				return new TitleImdbCell();
			}
		});
		System.out.println("ImdbSearchTitleResult<--");
	}

	public void setTitle(String title)
	{
		this.title = title;
		data.clear();
		data.addAll(ImdbTitleSearch.search(title));
	}

	public TitleResult getSelectedTitleResult()
	{
		return selectedTitleResult;
	}

	@FXML
	protected void selectAndClose(ActionEvent event)
	{
		selectedTitleResult = listResults.getSelectionModel().getSelectedItem();
		dialogStage.close();
	}

	@FXML
	protected void cancel(ActionEvent event)
	{
		dialogStage.close();
	}

	public void showInStage()
	{
		dialogStage = new Stage();
		dialogStage.setTitle("Edit Media");
		dialogStage.initModality(Modality.APPLICATION_MODAL);
		Scene scene = new Scene(this);
		dialogStage.setScene(scene);
		dialogStage.sizeToScene();
		dialogStage.showAndWait();
	}
}
